;
(function() {
  new Vue({
    el: '.businessGJ',
    data: {
      type: [{ name: '公司',code: 'QY'}, { name: '个人', code: 'PE'}],
      industryData: [],
      selectedIndustry:[],
      selectedIndustryVal:[],
      params: {
        themeInfo: {},
        zlfInfo: {}
      },
      intentLevel: [
        { name: '强烈', code: 'QL'},
        { name: '一般', code: 'YB'},
        { name: '无', code: 'NONE'},
        { name: '未知', code: 'NA'}
      ],
      intentLevelAct: 'QL',
      followupData: [
        { name: '客户上门', code: 'KHSM' },
        { name: '主动拜访', code: 'ZDBF' },
        { name: '客户来电', code: 'KHLD' },
        { name: '主动电访', code: 'ZDDF' },
        { name: '短信', code: 'SMS' },
        { name: '微信', code: 'WECHAT' },
        { name: '邮件', code: 'MAIL' },
        { name: '其他', code: 'OTHER'}
      ],
      selectedFollowup:[],
      memo: '',
      planfollowupcontent: '',
      sjId: hlUrlUtil.GetQueryString('businessId'),
      page: hlUrlUtil.GetQueryString('page') || 'businessMange',
      channelTypeData: [], //渠道分类
      selectedChannelType:[],
      selectedChannelTypeVal:[],
      channelNameData: [], //渠道名称
      selectedChannelName:[],
      selectedChannelNameVal:[],
      sourceData: [],
      selectedSource:[],
      selectedSourceVal:[],
      hour1: '',
      min1: '',
      hour2: '',
      min2: '',
      gjTime:'',
      jhTime:'',
    },
    mounted() {
      this.getIndustryData()
      this.getSourceData()
      this.getChannelTypeData()
      this.getChannelNameData()

      this.getDetails()
    },
    methods: {
      getIndustryData () {
        var self = this
        hlhttp.get({
          url: 'configure/industry/dict',
          success: data => {
            self.industryData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      selectedCustomerType(val) {
        this.params.zlfInfo.customerType = val;
      },
      getChannelTypeData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH',config_name: 'ZS_CHANNEL_CATE'},
          success: data => {
            self.channelTypeData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      getChannelNameData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_NAME' },
          success: data => {
            self.channelNameData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      getSourceData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH0SHJI', config_name: 'SJ_SOURCE' },
          success: data => {
            self.sourceData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      handleSourceChange(data){
        this.selectedSourceVal = data[0]['name'].split()
      },
      handleIndustryChange(data){
        this.selectedIndustryVal = data[0]['name'].split()
      },
      handleChannelTypeChange(data){
        this.selectedChannelTypeVal = data[0]['name'].split()
      },
      handleChannelNameChange(data){
        this.selectedChannelNameVal = data[0]['name'].split()
      },
      //获取线索详情
      getDetails() {
        var self = this
        hlhttp.get({
          url: 'customer/biz_oppt_info',
          data: { sj_id: self.sjId },
          success: res => {
            self.params = res;

            if(res.zlfInfo.industryCode) self.selectedIndustry.push(res.zlfInfo.industryCode)
            if(res.zlfInfo.industryName) self.selectedIndustryVal.push(res.zlfInfo.industryName)

            if(res.themeInfo.sourceCode) self.selectedSource.push(res.themeInfo.sourceCode)
            if(res.themeInfo.sourceValue) self.selectedSourceVal.push(res.themeInfo.sourceValue)

            if(res.themeInfo.channelCategoryCode) self.selectedChannelType.push(res.themeInfo.channelCategoryCode)
            if(res.themeInfo.channelCategoryValue) self.selectedChannelTypeVal.push(res.themeInfo.channelCategoryValue)
            if(res.themeInfo.channelNameCode) self.selectedChannelName.push(res.themeInfo.channelNameCode)
            if(res.themeInfo.channelNameValue) self.selectedChannelNameVal.push(res.themeInfo.channelNameValue)
          }
        })
      },
      //验证时间
      timerOs(i, n) {
        //i第几列  n为时分
        if (i == 0) {
          if (n == 0) {
            if (this.hour1 < 0) {
              hl.toast('最小时长不能小于0')
              this.hour1 = ''
            }
            if (this.hour1 > 23) {
              hl.toast('最大时长不能大于23')
              this.hour1 = ''
            }
          }
        }
        if (i == 0) {
          if (n == 1) {
            if (this.min1 < 0) {
              hl.toast('最小分长不能小于0')
              this.min1 = ''
            }
            if (this.min1 > 59) {
              hl.toast('最大分长不能大于59')
              this.min1 = ''
            }
          }
        }
        if (i == 1) {
          if (n == 0) {
            if (this.hour2 < 0) {
              hl.toast('最小时长不能小于0')
              this.hour2 = ''
            }
            if (this.hour2 > 23) {
              hl.toast('最大时长不能大于59')
              this.hour2 = ''
            }
          }
        }
        if (i == 1) {
          if (n == 1) {
            if (this.min2 < 0) {
              hl.toast('最小分长不能小于0')
              this.min2 = ''
            }
            if (this.min2 > 59) {
              hl.toast('最大分长不能大于59')
              this.min2 = ''
            }
          }
        }
      },
      //新建
      addBusiness() {
        if (!this.params.themeInfo.theme) {
          hl.toast('请填写主题')
          return
        }
        if (!this.params.zlfInfo.lperson) {
          hl.toast('请填写联系人')
          return
        }
        // if (!this.params.zlfInfo.phone) {
        //   hl.toast('请填写联系电话')
        //   return
        // }
        // this.params.zlfInfo.industryCode = this.industryAct || this.params.zlfInfo.industryCode;
        // this.params.zlfInfo.industryName = this.industryName || this.params.zlfInfo.industryName;
        // this.params.themeInfo.sourceCode = this.sourceCode || this.params.themeInfo.sourceCode;
        // this.params.themeInfo.sourceValue = this.sourceCodeKey || this.params.themeInfo.sourceValue;
        // this.params.themeInfo.channelCategoryCode = this.channerCodeKey || this.params.themeInfo.channelCategoryCode;
        // this.params.themeInfo.channelCategoryCode = this.channerCode || this.params.themeInfo.channelCategoryCode;
        // this.params.themeInfo.channelNameCode = this.channerNameCodeKey || this.params.themeInfo.channelNameCode;
        // this.params.themeInfo.channelNameValue = this.channerNameCode || this.params.themeInfo.channelNameValue;
        var time0, time1;

        function todou(n) {
          return n < 10 ? '0' + n : '' + n;
        }
        time0 = todou(this.hour1) + ':' + todou(this.min1) + ':00'
        time1 = todou(this.hour2) + ':' + todou(this.min2) + ':00'

        if (!this.gjTime) {
          hl.toast('跟进时间必填')
          return
        }
        if (!this.memo) {
          hl.toast('跟进内容必填')
          return
        }

        const themeInfo = {
          "theme": this.params.themeInfo.theme,
          "source_code": this.selectedSource.length ? this.selectedSource.join() : '#',
          "source_value": this.selectedSourceVal.length ? this.selectedSourceVal.join() : '#',
          "channel_category_code": this.selectedChannelType.length ? this.selectedChannelType.join() : '#',
          "channel_category_value": this.selectedChannelTypeVal.length ? this.selectedChannelTypeVal.join() : '#',
          "channel_name_code": this.selectedChannelName.length ? this.selectedChannelName.join() : '#',
          "channel_name_value": this.selectedChannelNameVal.length ? this.selectedChannelNameVal.join() : '#',
          "description": this.params.themeInfo.description || '#'
        }

        const zlfInfo = {
          "customer_name": this.params.zlfInfo.customerName || '#',
          "customer_type": this.params.zlfInfo.customerType || '#',
          "idcode": this.params.zlfInfo.idcode || '#',
          "industry_code": this.selectedIndustry.length ? this.selectedIndustry.join() : '#',
          "industry_value": this.selectedIndustryVal.length ? this.selectedIndustryVal.join() : '#',
          "lperson": this.params.zlfInfo.lperson || '#',
          "customer_addr": this.params.zlfInfo.customerAddress || '#',
          "phone": this.params.zlfInfo.phone || '',
          "email": this.params.zlfInfo.email || '#',
          "zipcode": this.params.zlfInfo.zipcode || '#'
        }

        const followUpInfo = {
          "intent_level": this.intentLevelAct,
          "followup_time": this.gjTime + ' ' + time0,
          "followup_type": this.selectedFollowup[0],
          "memo": this.memo,
          "plan_followup_time": this.jhTime + ' ' + time1,
          "plan_followup_content": this.planfollowupcontent
        }


        hlhttp.put({
          url: 'customer/biz_oppt_followup',
          data: {
            sj_id: this.sjId,
            theme_info: JSON.stringify(themeInfo),
            zlf_info: JSON.stringify(zlfInfo),
            follow_up_info: JSON.stringify(followUpInfo)
          },
          success: () => {
            hl.toast('跟进成功')
            hlUrlUtil.goTo('businessDetails', {
              'businessId': this.sjId,
              'page': hlUrlUtil.GetQueryString('page'),
              'origin': 'gj',
              'ddtab': true
            })
          }
        })

      }
    }


  })
}())
